package com.thoughtworks.collection;

import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import java.util.*;
import java.util.stream.Collectors;

public class Add {
    private boolean isOdd(int num) {
        return num % 2 != 0;
    }

    private boolean isEven(int num) {
        return num % 2 == 0;
    }

    private List<Integer> getEvenIndexArray(List<Integer> arr) {
        return arr.stream().filter(item -> this.isEven(arr.indexOf(item))).collect(Collectors.toList());
    }

    private List<Integer> getEvensArray(List<Integer> arr) {
        return arr.stream().filter(this::isEven).collect(Collectors.toList());
    }

    private List<Integer> getOddsArray(List<Integer> arr) {
        return arr.stream().filter(this::isOdd).collect(Collectors.toList());
    }

    private Integer getMedianOfArray(List<Integer> arr) {
        List<Integer> ascSortedArray = ascSortArray(arr);
        int len = ascSortedArray.size();
        return isEven(len) ?
                (ascSortedArray.get(len / 2) + ascSortedArray.get(ascSortedArray.get(len / 2) + 1)) / 2
                : ascSortedArray.get((len - 1) / 2);
    }

    private List<Integer> ascSortArray(List<Integer> arr) {
        return arr.stream().sorted().collect(Collectors.toList());
    }

    private List<Integer> descSortArray(List<Integer> arr) {
        return arr.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
    }

    private List<Integer> deDuplication(List<Integer> arr) {
        Set<Integer> collectionWithoutDuplicates = new HashSet<>(arr);
        return new ArrayList<>(collectionWithoutDuplicates);
    }

    public int getSumOfEvens(int leftBorder, int rightBorder) {
        int sum = 0;
        if (leftBorder < rightBorder) {
            for (int i = leftBorder; i <= rightBorder; i++) {
                if (i % 2 == 0) {
                    sum += i;

                }
            }

        } else {
            for (int i = rightBorder; i <= leftBorder; i++) {
                if (i % 2 == 0) {
                    sum += i;
                }
            }
        }
        return sum;
        /*throw new NotImplementedException();*/


    }

    public int getSumOfOdds(int leftBorder, int rightBorder) {
        int sum = 0;
        if (leftBorder < rightBorder) {
            for (int i = leftBorder; i <= rightBorder; i++) {
                if (i % 2 != 0) {
                    sum += i;
                }
            }
        } else {
            for (int i = rightBorder; i <= leftBorder; i++) {
                if (i % 2 != 0) {
                    sum += i;
                }
            }
        }
        return sum;
        /*throw new NotImplementedException();*/
    }

    public int getSumTripleAndAddTwo(List<Integer> arrayList) {
        int result = 0;
        /* List<Object> list = Arrays.asList();*/
        for (int i = 0; i < arrayList.size(); i++) {
            result += arrayList.get(i) * 3 + 2;
        }
        return result;
        /* throw new NotImplementedException();*/
    }

    public List<Integer> getTripleOfOddAndAddTwo(List<Integer> arrayList) {
        List<Integer> resultList = new ArrayList<>();
        int result = 0;
        for (int i = 0; i < arrayList.size(); i++) {
            if (arrayList.get(i) % 2 != 0) {
                result = arrayList.get(i) * 3 + 2;

            } else {
                result = arrayList.get(i);
            }
            resultList.add(result);
        }
        return resultList;
        /*  throw new NotImplementedException();*/
    }

    public int getSumOfProcessedOdds(List<Integer> arrayList) {
        int result = 0;
        for (int i = 0; i < arrayList.size(); i++) {
            if (arrayList.get(i) % 2 != 0) {
                result += (arrayList.get(i) * 3 + 5);
            }
        }
        return result;
    }

    public double getMedianOfEven(List<Integer> arrayList) {
        double list;
        int result = 0;
        int count = 0;
        for (int i = 0; i < arrayList.size(); i++) {
            if (arrayList.get(i) % 2 == 0) {
                count++;
                result += arrayList.get(i);
            }
        }
        list = result / count;
        return list;

    }

    public double getAverageOfEven(List<Integer> arrayList) {
        double sum = 0;
        int count = 0;
        for (int i = 0; i < arrayList.size(); i++) {
            if (arrayList.get(i) % 2 == 0) {
                sum += arrayList.get(i);
                count++;
            }

        }
        return sum / count;
        /* throw new NotImplementedException();*/
    }

    public boolean isIncludedInEvenIndex(List<Integer> arrayList, Integer specialElment) {
        List<Integer> Elment = new ArrayList<>();
        for (int i = 0; i < arrayList.size(); i++) {
            if (arrayList.get(i) % 2 == 0) {
                Elment.add(arrayList.get(i));
            }
        }
        return Elment.contains(specialElment);
    }

    //找出数组中的所有偶数，从中剔除重复数据并返
    public List<Integer> getUnrepeatedFromEvenIndex(List<Integer> arrayList) {
     /*   List<Integer> list=new ArrayList<>();

        for(int i=0;i<  arrayList.size();i++){
            if(arrayList.get(i)%2==0){
                list.add(arrayList.get(i));
                for(int j=0;j<list.size();j++){
                    if(arrayList.get(i)==list.get(j)){
                        break;
                    }
                }
            }
        }
        return list;*/

        List<Integer> evensArray = getEvensArray(arrayList);
        return deDuplication(evensArray);


    }

    public List<Integer> sortByEvenAndOdd(List<Integer> arrayList) {
        //排序数组 结果使数组偶数在递增在前，奇数递减在后
        /*List<Integer> list=new ArrayList<>();
        List<Integer> resultList=new ArrayList<>();
        for(int i=0;i<arrayList.size();i++){
            if(arrayList.get(i)%2==0){
                list.add(arrayList.get(i));


            }
        }
        for(int i=0;i<arrayList.size();i++){
            if(arrayList.get(i)%2!=0){
                list.add(arrayList.get(i));
                list.sort(arrayList.get(i),);
            }
        }

         */
        List<Integer> result = new ArrayList<>();
        List<Integer> evensArray = getEvensArray(arrayList);
        List<Integer> oddsArray = getOddsArray(arrayList);
        List<Integer> ascSortedEvenArray = ascSortArray(evensArray);
        List<Integer> descSortedOddArray = descSortArray(oddsArray);
        result.addAll(ascSortedEvenArray);
        result.addAll(descSortedOddArray);
        return result;
    }

    public List<Integer> getProcessedList(List<Integer> arrayList) {
        List<Integer> integerList = new ArrayList<>();
        int result = 0;
        for (int i = 0; i < arrayList.size(); i++) {
            if (arrayList.get(i) % 2 != 0) {
                result += arrayList.get(i) * 3 + 5;
                integerList.add(result);
            }

        }

        return integerList;
    }
}